package com.taikang.hmp.hbusiness.doctor.register.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;

import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.service.impl.BaseServiceImpl;
import com.taikang.hmp.frame.common.web.session.SessionBean;
import com.taikang.hmp.frame.customer.bean.CustomerInfo;
import com.taikang.hmp.frame.user.bean.AuthUserInfo;
import com.taikang.hmp.hbusiness.doctor.register.bean.Invitation;
import com.taikang.hmp.hbusiness.doctor.register.service.ILoginService;


/**
 * 类说明 :
 * @author :sym
 * @time : 2015-9-29 下午12:05:28
 */
public class LoginServiceImpl extends BaseServiceImpl implements ILoginService {
	
	/**
	 * 根据Uid获取Customerid
	 */
	public String getCustomerIdByUid(String uid)  {
		if(StringUtils.isEmpty(uid)){
			throw new TKException("uid is empty");
		}
		AuthUserInfo authInfo =(AuthUserInfo)super.getNewDao().queryForObject("hd_user_space.authInfo_uid",uid);
		if(authInfo == null){
			throw new TKException("uid is not exists");
		}
		
		return authInfo.getCustom_id();
	}
	
	
	/**
	 *手机号是否存在 
	 */
	public boolean check_mobile_repeat(String mobile) {
		//查找手机号
		List<?> mobile_info = super.getNewDao().queryForList("hd_user_space.check_mobile_repeat", mobile);
		if(mobile_info.size()>0){
			return true;//存在
		}else{
			return false;//不存在 
		}
	}

	/**
	 * 邀请码是否存在
	 */
	public boolean check_invitationcode_exist(String invitation_code) {
		//查找邀请码是否存在
		List<?> invitation_code_info = super.getNewDao().queryForList("hd_user_space.check_invitationcode_exist", invitation_code);
		if(invitation_code_info.size()>0){
			return false;//存在
		}else{
			return true;//不存在 
		}
	}

	/**
	 * 保存注册信息
	 */
	public Dto saveRegistInfo(Dto dto) {
		Dto resultDto = new BaseDto();
		CustomerInfo ci = new CustomerInfo();
		
		
		String customerSeq = (String) super.getNewDao().queryForObject("hd_user_space.getDCCustomerSeq","");
		String customer_id = "DC"+customerSeq;
		
		ci.setCustomer_id(customer_id);
		ci.setMobile(dto.getAsString("mobile"));
		ci.setSource_cd(dto.getAsString("requestsource"));
		super.getNewDao().insert("hd_user_space.insert_bs_customer", ci);
		
		AuthUserInfo aut = new AuthUserInfo();
		String uid = java.util.UUID.randomUUID().toString();
		aut.setUser_id(uid);
		aut.setUser_name((dto.getAsString("mobile")));
		aut.setUser_pwd(dto.getAsString("password"));
		aut.setCreate_dt(new Date());
		aut.setCustom_id(customer_id);
		aut.setOperate_date(new Date());
		aut.setOperater(uid);
		aut.setSource_cd(dto.getAsString("requestsource"));
		super.getNewDao().insert("hd_user_space.insert_auth_user", aut);
		
		resultDto.put("customerid", customer_id);
		resultDto.put("uid", uid);
		return resultDto;
	}
	
	
	/**
	 * 保存UCM用户信息
	 */
	public boolean saveUcmInfo(JSONObject jsonObj) {
		//customer_id,name,agentcode,agntcoy,agntbr,aracde,operate_date
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String invitationCode = jsonObj.getString("invitationCode");
		Map<String, String> info = new HashMap<String, String>();
		info.put("customer_id", jsonObj.getString("customerid"));
		info.put("name", jsonObj.getString("name"));
		info.put("agentstate", jsonObj.getString("agentstate"));
		info.put("agntcoyname", jsonObj.getString("agntcoyname"));
		info.put("agntbr", jsonObj.getString("agntbr"));
		info.put("agntbrname", jsonObj.getString("agntbrname"));
		info.put("aracde", jsonObj.getString("aracde"));
		info.put("aracdename", jsonObj.getString("aracdename"));
		info.put("register_time",format.format(new Date()) );
		info.put("agntcoy", invitationCode.substring(2, 3));
		info.put("agentcode", invitationCode.substring(3, invitationCode.length()));
		info.put("operate_date", new Date().toString());
		super.getNewDao().insert("hd_user_space.saveUcmInfo", info);
		super.getNewDao().insert("hd_user_space.saveImInfo", info);
		return true;
	}
	
	
	/**
	 * 获取客户信息到sessionBean
	 */
	public SessionBean getCustomerBasicInfo(String userid) {
		SessionBean sessionBean = new SessionBean();
		List<AuthUserInfo> info = super.getNewDao().queryForList("hd_user_space.authInfo_uid", userid);
		if(info==null||info.isEmpty()){
			throw new TKException("userid:"+userid+"无对应的信息");
		}
		System.out.println("info.get(0).getCustom_id()====="+info.get(0).getCustom_id());
		List<CustomerInfo> info1 = super.getNewDao().queryForList("hd_user_space.customer_uid", info.get(0).getCustom_id());
		sessionBean.setAuthUserInfo(info.get(0));
		sessionBean.setCustomerInfo(info1.get(0));
		return sessionBean;
	}

	
	
	/**
	 * 更新用户密码
	 */
	public Dto updateUserPwd(Dto dto)  {
		Dto returnInfo = new BaseDto();
		List<AuthUserInfo> info = super.getNewDao().queryForList("hd_user_space.getDLoginInfo", dto.getAsString("mobile"));
		if(info == null || info.size()==0){
			 returnInfo.put("mobileExist", "0");
			 return returnInfo;
		}
		AuthUserInfo info1 = new AuthUserInfo();
		info1.setUser_name(dto.getAsString("mobile"));
		info1.setUser_pwd(dto.getAsString("password"));
		
		
		try {
			super.getNewDao().update("hd_user_space.update_user_pwd", info1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<AuthUserInfo> info2 = super.getNewDao().queryForList("hd_user_space.authInfo_customerid", info.get(0).getCustom_id());
		returnInfo.put("mobileExist", "1");
		returnInfo.put("uid", info2.get(0).getUser_id());
		returnInfo.put("mobile", info2.get(0).getUser_name());
		return returnInfo;
	}

	/**
	 * 根据手机号获取用户信息 
	 */
	public List<AuthUserInfo> getLoginInfo(String mobile) {
		return super.getNewDao().queryForList("hd_user_space.getDLoginInfo", mobile);
	}

	
	
	
	
	
	
	public void saveBasicInfo(JSONObject jsonObj) throws Exception {
		String uid = jsonObj.getString("uid");
		AuthUserInfo aui = new AuthUserInfo();
		aui.setUser_id(uid);
		aui.setUser_name(jsonObj.getString("mobile"));
		aui.setUser_type(jsonObj.getString("userType"));
		super.getNewDao().update("hd_user_space.update_authUserInfo", uid);
		List<AuthUserInfo> authInfo =super.getNewDao().queryForList("hd_user_space.authInfo_uid",uid);
		CustomerInfo ci = new CustomerInfo();
		ci.setCustomer_id(authInfo.get(0).getCustom_id());
		ci.setName(jsonObj.getString("name"));
		ci.setBirthday(jsonObj.getString("birthday"));
		ci.setSex(jsonObj.getString("sex"));
		ci.setMobile(jsonObj.getString("mobile"));
		super.getNewDao().update("hd_user_space.update_customerInfo", ci);
	}

	
	
	public String getIdentify() {
		Random random = new Random();
		String result = "";
		for(int i=0;i<6;i++){
			result += random.nextInt(10);
		}
		return result;
	}

	@Override
	public Boolean check_moblie_exist(String mobile) {
		List<AuthUserInfo> au = super.getNewDao().queryForList("hd_user_space.check_moblie_exist",mobile); 
		if(au.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	

}
